package com.smart.buss.ctrl;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.smart.buss.service.BussAccountLogService;
import com.smart.buss.service.BussDakaLogService;
import com.smart.buss.service.BussDakaService;
import com.smart.buss.service.BussTaskLogService;
import com.smart.buss.service.BussTaskService;
import com.smart.buss.service.BussUserService;
import com.smart.primary.common.common.BaseCtrl;
import com.smart.primary.common.util.AjaxResult;
import com.smart.primary.common.util.StringUtil;
import com.smart.primary.sys.service.UserService;
import com.smart.primary.util.ContextHolderUtils;
import com.smart.primary.util.ServletUtil;

@RestController
@RequestMapping("/buss/user")
public class DeviceFrontController extends BaseCtrl {
	private final static Logger logger= Logger.getLogger(DeviceFrontController.class);

	@Autowired
	private BussUserService bussUserService;
	@Autowired
	private UserService sysUserService;
	@Autowired
	private BussDakaService bussDakaService;
	@Autowired
	private BussDakaLogService bussDakaLogService;
	@Autowired
	private BussAccountLogService bussAccountLogService;
	@Autowired
	private BussTaskService bussTaskService;
	@Autowired
	private BussTaskLogService bussTaskLogService;

	  
	 
	/**
	 * 
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/start/h5",produces="application/json")
	@ResponseBody
	public AjaxResult h5(HttpServletRequest request, HttpServletResponse response) {
		 
		JSONObject o = new JSONObject();
		String deviceToken = ContextHolderUtils.getDeviceToken(request);
		String channel = request.getParameter("channel");
		if(StringUtils.isEmpty(channel)) {
			channel = ServletUtil.getCookieValue(request, "channel");
		}
		String channelCode = ServletUtil.getCookieValue(request, "channel_code");
		try {
			if(StringUtil.isEmpty(deviceToken)) {
				deviceToken = UUID.randomUUID().toString();
			}
//			logger.info("h5 start config---deviceToken:"+deviceToken+",channel:"+channel);
//			List<DkDeviceEntity> list = null;
//			
//			if (StringUtils.isNotEmpty(deviceToken)){
////				String hql = "from DkDeviceEntity dk where dk.deviceToken='" + deviceToken + "'";
////				list = dkDeviceService.findByQueryString(hql);
//			}
			
//			if (StringUtils.isEmpty(deviceToken) || list.size()==0) {
				
//				DkDeviceEntity dk = new DkDeviceEntity();
//
//				if (StringUtils.isEmpty(deviceToken) || list.size() == 0) {
//					if(StringUtils.isEmpty(deviceToken)) {
//						deviceToken = UUID.randomUUID().toString();
//					}
//					dk.setChannel(channel);
//					dk.setDevice("h5");
//					dk.setChannelCode(channelCode);
//					dk.setDeviceToken(deviceToken);
//					dk.setCreateDate(new Date());
//					String createDateIndex = DateUtils.formatDate( "yyyyMMdd");
//					dk.setCreateDateIndex(createDateIndex);
//					dkDeviceService.save(dk);
//
//				} else {
//				}
			
				ServletUtil.addCookie(response, ContextHolderUtils.deviceToken, deviceToken);
				
//			}
		} catch (Exception e) {
			e.printStackTrace();

		}
		o.put(ContextHolderUtils.deviceToken, deviceToken);
		return AjaxResult.success(o);
		
	}

}
